<?php
!defined('P_W') && exit('Forbidden');
L::loadClass('upload', '', false);

/**
 * 上传图片控制类
 * @author bingbing.renbb@2010-8-28
 *
 */
class ImageUpload extends uploadBehavior {
	/**
	 * @var array $ftype 允许的后缀及大小
	 * @var int   $ifftp 是否使用ftp上传
	 * @var int	  $houseid  图片所属的房子ID
	 * @var array $attachs 包含图片上传信息的数组
	 * @var int	  $isRent 是否是租房图片上传
	 * @var int   $_allowThumb 是否允许缩略图
	 */
	var $ftype;
	var $ifftp;
	var $houseid;
	var $attachs;
	var $isRent;
	var $_allowThumb;
	
	/**
	 * 构造函数
	 */
	function ImageUpload ($isRent = 'sale',$houseid = 0) {
		$this->__construct($isRent,$houseid);
	}
	
	function __construct($isRent = 'sale', $houseid = 0) {
		global $db_ifftp, $db_ifathumb;
		parent::uploadBehavior();
		$this->houseid = $houseid;
		$this->ifftp = $db_ifftp;
		$this->ftype = array('gif' => 2000, 'jpg' => 2000, 'jpeg' => 2000, 'bmp' => 2000, 'png' => 2000);	
		$this->isRent = $isRent;
		$this->_allowThumb = 1;
	}
	
	function allowType($key) {
		return true;
	}
	
	/**
	 * 设置文件名，缩略图名，保存路径
	 * @param array $currUpload 包含上传信息的数组
	 * @return array 包含名称，路径信息的数组
	 */
	function getFilePath($currUpload) {
		global $timestamp,$attachdir;
		$filename = substr(md5(rand(1, 100) . $timestamp . $currUpload['name']), 10, 10) . '.' . $currUpload['ext'];
		$savedir = 'salepic/';
		$thumbname = $filename;
		$thumbdir = 'salethumbpic/';
		return array($filename, $savedir, $thumbname, $thumbdir);
	}
	
	/**
	 * 是否允许缩略图
	 * @return bool
	 */
	function allowThumb() {
		if ($this->_allowThumb == 0) return false;
		$this->_allowThumb = 0;
		return true;
	}
	
	/**
	 * 缩略图的大小
	 * @return string 缩略图的大小
	 */
	function getThumbSize() {
		return "100\t100";
	}
	
	/**
	 * 是否允许水印
	 * @return bool 不允许水印
	 */
	function allowWaterMark() {
		return false;
	}
	
	/**
	 * 将图片信息插入数据库
	 * @param $uploaddb 包含图片上传信息的数组
	 */
	function update($uploaddb) {
		$imageService = $this->_getImageService();
		$insertDB = array();
		foreach ($uploaddb as $key => $value) {
			$insertDB[] = $this->_replace($value);
		}
		$this->attachs = $insertDB;
		$imageService->addMulti($insertDB);
	}
	
	/**
	 * 取得第一张图片的缩略图
	 * @param mixed $attachs 图片上传信息的数组
	 * @return string 缩略图路径
	 */
	function getFirstThumbPic($attachs = null){
		$this->attachs = $attachs ? $attachs : $this->attachs;
		if($this->attachs){
			$first = current($this->attachs);
		}
		$thumbDir = 'salethumbpic/';
		$thumbName = substr($first['fileuploadurl'], 8, strlen($first['fileuploadurl']) - 8);
		if ($thumbName == '') return '';
		return $thumbDir . $thumbName;	
	}
	
	/**
	 * 处理数据
	 * @param array $value 图片信息数组
	 * @return array $temp 处理后的数据
	 */
	function _replace($value) {
		global $windid, $timestamp;
		$temp = array();
		if (!S::isArray($value)) return array();
		$temp['type'] = $this->isRent == 'sale' ? 0 : 1;
		$temp['houseid'] = $value['houseid'] ? $value['houseid'] : $this->houseid;
		$temp['size'] = $value['size'];
		$temp['path'] = $value['fileuploadurl'];
		$temp['uploadtime'] = $timestamp;
		$temp['descrip'] = Char_cv(GetGP('atc_desc_'.$value['id']));
		$temp['creator'] = $windid;
		$temp['name'] = substr($value['fileuploadurl'], 8, strlen($value['fileuploadurl']) - 8);
		$source = $this->_savePath($temp['name'], 'salepic/');
		$this->_waterMark($source, $value['ext']);
		return $temp;
	}
	
	/**
	 * 为大图加水印
	 * @param string $source
	 * @param string  $ext
	 * @param mixed $imgsize
	 */
	function _waterMark($source, $ext, $imgsize = null) {
		global $db_watermark, $db_waterwidth, $db_waterheight, $db_ifgif, $db_waterimg, $db_waterpos, $db_watertext, $db_waterfont, $db_watercolor, $db_waterpct, $db_jpgquality;
		empty($imgsize) && $imgsize = GetImgSize($source, $ext);
		if (empty($imgsize)) return false;
		if ($db_watermark && $imgsize['type'] < 4 && $imgsize['width'] > $db_waterwidth && $imgsize['height'] > $db_waterheight && function_exists('imagecreatefromgif') && function_exists('imagealphablending') && ($ext != 'gif' || function_exists('imagegif') && ($db_ifgif == 2 || $db_ifgif == 1 && (PHP_VERSION > '4.4.2' && PHP_VERSION < '5' || PHP_VERSION > '5.1.4'))) && ($db_waterimg && function_exists('imagecopymerge') || !$db_waterimg && function_exists('imagettfbbox'))) {
			ImgWaterMark($source, $db_waterpos, $db_waterimg, $db_watertext, $db_waterfont, $db_watercolor, $db_waterpct, $db_jpgquality);
		}
	}
	
	/**
	 * 图片保存路径
	 * @param string $filename
	 * @param string $dir
	 * @return string $source
	 */
	function _savePath($filename, $dir) {
		global $attachdir;
		if ($this->ifftp) {
			$source = D_P . "data/tmp/{$filename}";
		} else {
			$source = $attachdir . '/' . $dir . $filename;
		}
		return $source;
	}
	
	/**
	 * 获取图片服务
	 */
	function _getImageService() {
		return House::loadService('secondImageService');
	}
}
?>